import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import Son from "../components/Homes/Son.vue"
import Music from "../components/Homes/Music"
import Hot from "../components/Homes/Hot"
import Search from "../components/Homes/Search"
import Detail from "../views/Detail"
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    istoken:false,
    component: Home,
    redirect:"son/music", //不要加
    children:[
      {
        path:'son',
        name:"son",
        redirect:"son/music",
        component:Son,
        children:[
          {path:"music",component:Music,name:"music"},
          {path:"hot",component:Hot,name:"hot"},
          {path:"search",component:Search,name:"search"},
        ]
      }
    ]
  },
  {
    path:"/detail",
    name:"Detail",
    istoken:true,
    component:Detail
  },
  {
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue"),
  },
  {
    path:"/mine",
    istoken:false,
    name:"Mine",
    component:() => import("../views/Mine")
  },
  {
    path:"/boke",
    istoken:false,
    name:"Boke",
    component:() => import("../views/Boke")
  },
  {
    path:"/guan",
    istoken:false,
    name:"Guan",
    component:() => import("../views/Guan")
  },
  {
    path:"/yun",
    istoken:false,
    name:"Yun",
    component:() => import("../views/Yun")
  },
  
];

const router = new VueRouter({
  routes,
});
//权限管理
router.beforeEach(function(to,from,next){
  console.log(to,from)
  let token = localStorage.getItem("token")
  if(to.name=="Mine"){//登录页
    next()
  }else if(to.name==="Detail"){
    if(token){
      next()
    }else{
      next({name:"Mine"})
    }
  }else{
    next()
  }
})
  // next()

  // let routes = router.options.routes//获取路由的所有配置项
  // let tokenroutes = routes.filter((item)=>{
  //   return item.istoken==true 
  // })
  // console.log(tokenroutes)//需要配置权限的路由配置项
 


//
export default router;
